﻿Imports CObject
Imports System.Data.SqlClient
Imports ConnectionServer
Namespace CMT

    Public Class Group
        Inherits CObject.CObject

#Region "Override Properties"

        ''' <summary>
        ''' The override property to set up the connection string
        ''' </summary>
        ''' <value>The source database</value>
        ''' <returns>The connection string</returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property SourceDataBase() As ConnectionServer.Connection.SourceDataBase
            Get
                Return ConnectionServer.Connection.SourceDataBase.CMT
            End Get
        End Property

        ''' <summary>
        ''' Function to return the clone of the
        ''' object Group
        ''' </summary>
        ''' <returns>A new group boject</returns>
        ''' <remarks></remarks>
        Public Overrides Function Clone() As CObject.CObject
            Return New Group
        End Function

        ''' <summary>
        ''' Property to get the primary key of
        ''' the "Group" object.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property Key() As String
            Get
                Return "GroupID"
            End Get
        End Property

#End Region
#Region "Override Functions"

        ''' <summary>
        ''' Function to execute the inline queries related to the
        ''' group object
        ''' </summary>
        ''' <param name="peCommandType"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "SELECT GroupID as GroupID,Groups as Name, Descr as Description, ContactLevel as ContactLevel from SEC_Groups"
                Case CommandType.SQLUpdate
                    Return "UPDATE SEC_Groups SET Groups = {Name}, Descr = {Description}, ContactLevel = {ContactLevel} where GroupID = {GroupID}"
                Case CommandType.SQLDelete
                    Return "DELETE FROM SEC_Groups where GroupID = {GroupID}"
                Case CommandType.SQLInsert
                    Return "INSERT INTO SEC_Groups(Groups,Descr,ContactLevel) Values ({Name},{Description},{ContactLevel})"
                Case Else
                    Throw New Exception("Invalid commandType")
            End Select
        End Function
#End Region
#Region "Properties"

        Public Property GroupID() As Integer
            Get
                Return CastToInteger(GetProperty("GroupID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("GroupID", value)
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the Group Description
        ''' </summary>
        ''' <value>The group description value</value>
        ''' <returns>The description value</returns>
        ''' <remarks></remarks>
        Public Property Description() As String
            Get
                If Not IsDBNull(GetProperty("Description")) Then
                    Return CastToString(GetProperty("Description"))
                Else
                    Return ""
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("Description", Value)
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the Group Contact Level
        ''' </summary>
        ''' <value>The group description value</value>
        ''' <returns>The description value</returns>
        ''' <remarks></remarks>
        Public Property ContactLevel() As Int32
            Get
                If Not IsDBNull(GetProperty("ContactLevel")) Then
                    Return CastToInteger(GetProperty("ContactLevel"))
                Else
                    Return 0
                End If
            End Get
            Set(ByVal Value As Int32)
                SetProperty("ContactLevel", Value)
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the Group Name
        ''' </summary>
        ''' <value>The group name</value>
        ''' <returns>The group name</returns>
        ''' <remarks></remarks>
        Public Property Name() As String
            Get
                If Not IsDBNull(GetProperty("Name")) Then
                    Return CastToString(GetProperty("Name"))
                Else
                    Return ""
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("Name", Value)
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the Users associated with
        ''' the group
        ''' </summary>
        ''' <value>The Users associated with the group</value>
        ''' <returns>The group associated users</returns>
        ''' <remarks></remarks>
        Public Property UserRights() As DataTable
            Get
                Dim loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Return loConn.GetDataTable("SELECT R.RightsID, R.GUI, EP.Last_Name+ ' '+ EP.First_Name as Name FROM sec_user_Rights R join CMT_Employee EP on R.GUI = EP.GUI WHERE R.GroupID = " & GroupID)
            End Get
            Set(ByVal Value As DataTable)
                SetProperty("UserRights", Value)
            End Set
        End Property

#End Region
    End Class
End Namespace
